#ifndef SOLUTION_H_
#define SOLUTION_H_
#include <string>
#include <sstream>
using namespace std;

class Solution {
public:
    string countAndSay(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function

		if(n<=0)
			return "";

		if(1==n)
			return "1";


		ostringstream oss;
		string result = "1";
		for(int i=2;i<=n;i++){
			oss.str("");
			next(result,oss);
			result = oss.str();
		}

		return result;        
    }

	void next(const string& curStr,ostringstream& oss){
		char curC = curStr[0];
		int curNum = 0;

		for(size_t i=0;i<curStr.size();i++){
			if(curStr[i]==curC){
				curNum++;
			}
			else{
				oss<<curNum<<curC;
				curC = curStr[i];
				curNum = 1;
			}
		}

		if(curNum){
			oss<<curNum<<curC;
		}
	}
};
#endif